﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PI_CS_Tools;

namespace  PI_CS_Library.Virtual_Network_Information_Model
{
    public class Matrix
    {
        protected Identification _id;
        protected string _name;
        protected string _type;
        protected float _positionX;
        protected float _positionY;

        Dictionary<int, Link> _outgoingLinkList;
        Dictionary<int, Link> _incomingLinkList;

        protected Dictionary<int, IMatrixConnection> _matrixConnectionList;


        protected Dictionary<int, NwTTP> _nwTtpList;

        /*
        protected Dictionary<string, NwCTP> _nwCtpList;

        protected Dictionary<string, CTP> _CtpList;
        */

        protected MatrixParameters _parameters;

        public Matrix(MatrixParameters _parameters)
        {
            this._parameters = _parameters;

            _outgoingLinkList = new Dictionary<int, Link>();
            _incomingLinkList = new Dictionary<int, Link>();

            _matrixConnectionList = new Dictionary<int, IMatrixConnection>();

            _nwTtpList = new Dictionary<int, NwTTP>();
        }

        public Matrix(List<Link> _incomingLinks, List<Link> _outgoingLinks, MatrixParameters _parameters)
        {
            _id = new Identification("Matrix:");
            _outgoingLinkList = new Dictionary<int, Link>();
            _incomingLinkList = new Dictionary<int, Link>();

            foreach (Link _link in _incomingLinks)
            {
                _incomingLinkList.Add(_link.Id.Number, _link);
            }

            foreach (Link _link in _outgoingLinks)
            {
                _outgoingLinkList.Add(_link.Id.Number, _link);
            }

            this._parameters = _parameters;

            _matrixConnectionList = new Dictionary<int, IMatrixConnection>();

            _nwTtpList = new Dictionary<int, NwTTP>();
        }

        public Matrix()
        {
            _id = new Identification("Matrix:");
            _parameters = new MatrixParameters();

            _outgoingLinkList = new Dictionary<int, Link>();
            _incomingLinkList = new Dictionary<int, Link>();

            _matrixConnectionList = new Dictionary<int, IMatrixConnection>();

            _nwTtpList = new Dictionary<int, NwTTP>();
        }

        // Copy constructor
        public Matrix(Matrix _oldMatrix)
        {
            _id = new Identification(_oldMatrix._id);

            _outgoingLinkList = new Dictionary<int, Link>(_oldMatrix._outgoingLinkList);
            _incomingLinkList = new Dictionary<int, Link>(_oldMatrix._incomingLinkList);

            _matrixConnectionList = new Dictionary<int, IMatrixConnection>(_oldMatrix._matrixConnectionList);

            _parameters = new MatrixParameters(_oldMatrix._parameters);

            _nwTtpList = new Dictionary<int, NwTTP>(_oldMatrix._nwTtpList);
        }

        public Identification Id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }

        public float PositionX
        {
            get { return _positionX; }
            set { _positionX = value; }
        }

        public float PositionY
        {
            get { return _positionY; }
            set { _positionY = value; }
        }

        public Dictionary<int, Link> OutgoingLinkList
        {
            get { return _outgoingLinkList; }
            set { _outgoingLinkList = value; }
        }

        public Dictionary<int, Link> IncomingLinkList
        {
            get { return _incomingLinkList; }
            set { _incomingLinkList = value; }
        }

        public Dictionary<int, NwTTP> NwTtpList
        {
            get { return _nwTtpList; }
            set { _nwTtpList = value; }
        }

        public NwTTP AddNwTTP(int _ttpId, int _port, Direction _dir)
        {
            NwTTP _newNwTTP = new NwTTP(_ttpId, _port, _dir);

            _nwTtpList.Add(_newNwTTP.Id.Number, _newNwTTP);

            return _newNwTTP;
        }

        public NwTTP GetNwTTP(int _id)
        {
            return _nwTtpList[_id];
        }

        public void AddMatrixConnection<SrcPointType, SinkPointType>(MatrixConnection<SrcPointType, SinkPointType> item)
        {
            _matrixConnectionList[item.Id.Number] = item;

            item.MatrixId = Id;
            //if (item.SrcPoint.GetType == NwTTP) _nwTtpList.Add(item.SrcPoint.ToString(), (NwTTP) item.SrcPoint);

        }

        public MatrixConnection<SrcPointType, SinkPointType> GetMatrixConnection<SrcPointType, SinkPointType>(int _id)
        {
            return _matrixConnectionList[_id] as MatrixConnection<SrcPointType, SinkPointType>;
        }

        public void RemoveMatrixConnection(int _id)
        {
            _matrixConnectionList.Remove(_id);
        }

        public MatrixParameters Parameters
        {
            get { return _parameters; }
            set { _parameters = value; }
        }

        public override string ToString()
        {
            return _id.ToString();
        }
    }
}
